<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//
// Method Call : script_tag('jquery/jquery.min.js')
// Output      : <script type="text/javascript" src="/js/jquery/jquery.min.js"></script>
//
if ( ! function_exists('script_tag'))
{
	function script_tag($src, $type = 'text/javascript')
	{
		if ( ! $src)
			return;
		
		$val = '';
		
		if (is_array($src))
		{
			foreach($src as $item)
				$val .= script_tag($item); 
			
			return $val;
			
		}
		
		return '<script type="' . $type . '" src="/js/' . $src . '"></script>';
	}
	
}

if ( ! function_exists('css_tag'))
{
	function css_tag($src)
	{
		if ( ! $src)
			return;

		$val = '';

		if (is_array($src))
		{
			foreach($src as $item)
				$val .= link_tag('css/' . $item);
					
			return $val;	
		}

		return link_tag('css/' . $item);
	}

}

if ( ! function_exists('less_tag'))
{
	function less_tag($src)
	{
		if ( ! $src)
			return;

		$val = '';

		if (is_array($src))
		{
			foreach($src as $item)
				$val .= link_tag('less/' . $item);
				
			return $val;
		}

		return link_tag('less/' . $item);
	}

}

if ( ! function_exists('dump'))
{
        function dump($var)
        {
                $backtrace = debug_backtrace();
                echo 'In function <span style="font-weight: bold;">' . $backtrace[1]['class'] . '->' . $backtrace[1]['function'] . "</span><br />\n";
                echo '<pre>';
                echo php_encode($var, TRUE, '    ');
                echo '</pre>';
                
                return $var;
        }
}

if ( ! function_exists('dumpd'))
{
        function dumpd($var)
        {
                $backtrace = debug_backtrace();
                echo 'In function <span style="font-weight: bold;">' . $backtrace[1]['class'] . '->' . $backtrace[1]['function'] . "</span><br />\n";
                echo '<pre>';
                echo php_encode($var, TRUE, '    ');
                echo '</pre>';
                
                die();
        }
}

if ( ! function_exists('php_encode'))
{
	function php_encode($value, $formatted = FALSE, $indent_char = "\t", $indent = 0, $no_starting_indent = FALSE)
	{
		if (is_object($value))
			throw new Exception('php_encode does not support objects');
		
		if ($formatted && ! $no_starting_indent)
			$retval = str_repeat($indent_char, $indent);
		else
			$retval = '';
		
		if (is_null($value))
			return $retval . 'NULL';
		
		if (is_bool($value))
			return $retval . $value ? 'TRUE' : 'FALSE';
		
		if (is_integer($value))
			return $retval . $value;
		
		if (is_float($value))
			return $retval . $value;
		
		if (is_string($value))
			return $retval . "'" . addslashes($value) . "'";
		
		if (is_array($value))
		{
			// if array is not associative
			$array_is_associative = (array_keys($value) === range(0, count($value) - 1));
			
			if (empty($value))
				return $retval . 'array()';
			
			$retval .= "array(";
			
			if ($formatted)
				$retval .= "\n";
			
			foreach ($value as $key => $array_value)
			{
				if ( ! $array_is_associative)
					$retval .= php_encode($key, $formatted, $indent_char, $indent + 1) . ' => ';
				
				// build array value
				$retval .= php_encode($array_value, $formatted, $indent_char, $indent + 1, ! $array_is_associative) . ',';
				
				if ($formatted)
					$retval .= "\n";
				else
					$retval .= ' ';
			}
			
			$retval = substr($retval, 0, -2);
			
			if ($formatted)
				$retval .= "\n" . str_repeat($indent_char, $indent);
			
			$retval .= ")";
			
			return $retval;
		}
		
		return 'type not supported';
	}
}